package com.weirong.smartvault.backservice.entity_model;

import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.ConfirmDetailLogRepository;
import com.weirong.smartvault.dao.DepartmentRepository;
import com.weirong.smartvault.entity.ConfirmDetailLog;
import com.weirong.smartvault.entity.ConfirmLog;
import com.weirong.smartvault.backservice.user.IUser;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kfeb4
 * Date: 2017/12/27
 * Time: 11:07
 */
public class ConfirmLogModel {
    private ConfirmLog confirmLog;
    ConfirmDetailLogRepository confirmDetailLogRepository;
    DepartmentRepository departmentRepository;
    public ConfirmLogModel(ConfirmLog confirmLog){
        this.confirmLog = confirmLog;
        this.confirmDetailLogRepository = SpringContextUtil.getBean(ConfirmDetailLogRepository.class);
        this.departmentRepository = SpringContextUtil.getBean(DepartmentRepository.class);
    }
    public Collection<MoneyLogCollectionModel<MoneyLogModel>> getRequests() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, NoSuchFieldException, InstantiationException {
        //获取当前的需求信息
        Integer id = confirmLog.getId();
        List<ConfirmDetailLog> logs = confirmDetailLogRepository.findAllByConfirmId(id);
        List<MoneyLogModel> logModels = new ArrayList<>();
        if (logs.size()!=0) {
            ConfirmDetailLogModel logModel = new ConfirmDetailLogModel(logs.get(0));
            logModels = logModel.getRequestDetail();
        }
        return MoneyLogCollectionModel.makeCollection(logModels);

    }
    public List<ConfirmDetailLogModel> getConfirmDetailLogModel() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        //获取各个包的核账信息
        Integer id = confirmLog.getId();
        List<ConfirmDetailLog> logs = confirmDetailLogRepository.findAllByConfirmId(id);
        return ListTool.cast(logs,ConfirmDetailLogModel.class);
    }

    public String getDepartmentName(){
        Integer id = confirmLog.getBranchId();
        return new DepartmentModel(departmentRepository.findOne(id)).getName();
    }

    public String getHandlerName(){
        Integer id = confirmLog.getHandler();
        return IUser.findOne(id).getUserName();
    }

    public String getConfirmTimeExpress(){
        Date date = confirmLog.getConfirmTime();
        return DateUtil.getNomalString(date);
    }

    public Integer getId(){
        return confirmLog.getId();
    }

    public Boolean getConfirmResult(){
        List<ConfirmDetailLog> detailLogs = confirmDetailLogRepository.findAllByConfirmId(getId());
        for(ConfirmDetailLog log:detailLogs){
            if(!log.getConfirmResult().equals("success")){
                return false;
            }
        }
        return true;
    }
}
